package scjp.preparation.chapter123;

import java.util.HashMap;
import java.util.Map;

public enum DayOfWeek {
	MONDAY(1,"4 days till Friday"),
	TUESDAY(2,"3 days till Friday"),
	WEDNESDAY(3,"2 days till Friday"),
	THURSDAY(4,"1 days till Friday"),
	FRIDAY(5,"Friday! Let's go out!"),
	SATURDAY(6,"...."),
	SUNDAY(7,"Damn... Monday is coming");
	
	private int sequence;
	private String descr;
	
	private DayOfWeek(){}
	
	private static Map<Integer,DayOfWeek> codeToDayOfWeek;
		
	private static void initMapping(){
		codeToDayOfWeek = new HashMap<Integer,DayOfWeek>();
		for(DayOfWeek d : values()){
			codeToDayOfWeek.put(d.sequence, d);
		}
		
	}
	
	/*
	 * Learnt that you can only access static 
	 */
	public static DayOfWeek getDayOfWeek(int i){
		if(codeToDayOfWeek == null){
			initMapping();
		}
		DayOfWeek days = null;
		for(DayOfWeek d : values()){
			days = codeToDayOfWeek.get(i);
		}
		return days;
	}
	
	public static DayOfWeek[] getAllDayOfWeek(){
		return DayOfWeek.values();
	}
	
	private DayOfWeek(int code, String descr){
		this.sequence = code;
		this.descr = descr;
	}
	public int getCode() {
		return sequence;
	}
	public void setCode(int code) {
		this.sequence = code;
	}
	public String getDescr() {
		return descr;
	}
	public void setDescr(String descr) {
		this.descr = descr;
	}
	
	@Override
	public String toString(){
		final StringBuilder sb = new StringBuilder();
		sb.append("Day of Week")
		.append("{sequence=")
		.append(sequence)
		.append(", description=")
		.append(descr)
		.append("}");
		return sb.toString();
	}

	

	
}
